package com.zygame.sdk.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zygame.sdk.CmCode;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.utils.LoginUtil;
import com.zygame.sdk.utils.VerifyUtil;
import com.zygame.sdk.utils.http.HttpUtil;
import com.zygame.sdk.utils.http.NetHttpUtil;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.utils.phone.AppUtil;
import com.zygame.sdk.utils.res.ResourceUtil;
import com.zygame.sdk.utils.toast.ToastUtil;
import com.zygame.sdk.view.ImageEditText;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Yuxl
 * 一键注册
 * */

public class ZY_Dialog_QRegister extends ZY_BaseDialog {

    private Context mContext;

    private String mAccount;
    private String mPwd;

    private ImageEditText et_account;
    private ImageEditText et_pwd;


    public ZY_Dialog_QRegister(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected void init() {
        //统计注册页面
        showSoft();
        showBack(true);
        showClose(true);
        et_account = (ImageEditText) findViewById(ResourceUtil.getId(mContext, "et_account"));
        et_pwd = (ImageEditText) findViewById(ResourceUtil.getId(mContext, "et_pwd"));

        et_account.setTextViewBG("fy_input_account");
        et_account.setHintid("GSDK_email_text");
        et_account.setLineHide();
        et_account.setContentClole("#000000");
        et_account.hideHidstoryImageView();
        et_account.hideDelete();
        et_account.hidePromptText();

        et_pwd.setTextViewBG("fy_input_pwd");
        et_pwd.setHintid("GSDK_password_placeholaer");
        et_pwd.hidePromptGone();
        et_pwd.hideHidstoryImageView();
        et_pwd.setLineHide();
        et_pwd.setContentClole("#000000");
        et_pwd.setIcon(mContext.getResources().getDrawable(ResourceUtil.getDrawableId(mContext, "fy_pwd")));
        et_pwd.hideDelete();

        Button btn_login = (Button) findViewById(ResourceUtil.getId(mContext, "btn_login"));
        findViewById(ResourceUtil.getId(mContext, "tv_quick_register")).setVisibility(View.GONE);
        findViewById(ResourceUtil.getId(mContext, "tv_des")).setVisibility(View.VISIBLE);
        findViewById(ResourceUtil.getId(mContext, "view")).setVisibility(View.VISIBLE);

        btn_login.setOnClickListener(this);

        HttpUtil.getName(mContext, onGetGameCallBac);

    }

    @Override
    public void show() {
        super.show();
        showLoad(true);
    }

    @Override
    protected String getResourceStr() {
        return "zy_dialog_register";
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();
        if (id == ResourceUtil.getId(mContext, "btn_back")) {
            new ZY_Dialog_Register(mContext).show();
            ZyGameSDK.getInstance().setDialogDismissFlag(CmCode.CODE_LOGIN_CANCEL);
            ZY_Dialog_QRegister.this.dismiss();
        } else if (id == ResourceUtil.getId(mContext, "btn_login")) {
            mAccount = et_account.getContent().trim();
            mPwd = et_pwd.getContent().trim();
            if (mAccount.isEmpty() || mPwd.isEmpty()) {
                et_account.setPromptTextId("GSDK_UserApassword_error");
                return;
            }
            if (AppUtil.txtLen(mAccount)) {
                et_account.setPromptTextId("GSDK_username_error");
                return;
            }
            if (AppUtil.txtLen(mPwd)) {
                et_account.setPromptTextId("GSDK_password_error");
                return;
            }
            showLoad(true);
            HttpUtil.reg(mContext, onRegCallback, mAccount, mPwd, "2");
        }

    }




    NetHttpUtil.DataCallback<JSONObject> onRegCallback = new NetHttpUtil.DataCallback<JSONObject>() {
        @Override
        public void callbackSuccess(JSONObject paramObject) {

            showLoad(false);
            LogHelper.i("注册接口", paramObject.toString());
            String code = paramObject.optString("code").trim();
            if ("0".equals(code)) {
                LoginUtil.loginResult(mAccount, mPwd, paramObject,mContext,"1",1,"");
                Map<String, String> map = new HashMap<String, String>();
                map.put("mUserName", mAccount);
                map.put("mPassword", mPwd);
                new SaveAccountView(mContext, map);
                Toast.makeText(mContext,ResourceUtil.getStringId(mContext,"GSDK_savealbum_text"),Toast.LENGTH_SHORT).show();
            }else {
                ToastUtil.showToast(mContext, paramObject.optString("message"));
            }
        }

        @Override
        public void callbackError(String error) {
            showLoad(false);
            ToastUtil.showToast(mContext, error);
        }
    };

    NetHttpUtil.DataCallback<JSONObject> onGetGameCallBac = new NetHttpUtil.DataCallback<JSONObject>() {
        @Override
        public void callbackSuccess(JSONObject paramObject) {
            String code = paramObject.optString("code").trim();
            showLoad(false);
            if ("0".equals(code)) {
                String randomPassword = VerifyUtil.getRandomPassword(6);
                String user_name = paramObject.optString("user_name");
                et_account.setContent(user_name);
                et_pwd.setContent(randomPassword);
            } else {
                ToastUtil.showToast(mContext, paramObject.optString("message"));
            }
        }

        @Override
        public void callbackError(String error) {

        }
    };


}
